﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace jzgl
{
    public partial class UsersWindow : Form
    {
        FlowLayoutPanel[] FLP = new FlowLayoutPanel[3];
        Panel[] PA = new Panel[4];
        int licensenum;
        public UsersWindow(int licensenum)
        {
            InitializeComponent();
            FLP[0] = CXPanel;
            FLP[1] = FDPanel;
            FLP[2] = SWPanel;
            PA[0] = panel1;
            PA[1] = panel2;
            PA[2] = panel3;
            PA[3] = panel4;
            this.licensenum = licensenum;
        }
        private void ExitButton_Click(object sender, EventArgs e)//退出按钮
        {
            DialogResult dr = MessageBox.Show("确定要退出？", "退出", MessageBoxButtons.YesNo);
            if (dr == DialogResult.Yes)
            {
                Application.Exit();
            }
        }
        private void button1_Click(object sender, EventArgs e)//注销按钮
        {
            Application.Restart();
        }
        private void CXButton_Click(object sender, EventArgs e)//导航栏---查询
        {
            CXPanel.Visible = !CXPanel.Visible;
            panel1.Visible = !panel1.Visible;
            for (int i = 0; i < 3; i++)
            {
                if(FLP[i].Name!="CXPanel")
                    FLP[i].Visible = false;
            }
            for (int i=0;i<4 ;i++ ) 
            {
                if (PA[i].Name != "panel1")
                    PA[i].Visible = false;
            }
        }
        private void FDButton_Click(object sender, EventArgs e)//导航栏---罚单
        {
            FDPanel.Visible = !FDPanel.Visible;
            for (int i = 0; i < 3; i++)
            {
                if (FLP[i].Name != "FDPanel")
                    FLP[i].Visible = false;
            }
            for (int i = 0; i < 4; i++)
            {
                if (PA[i].Name != "panel2")
                    PA[i].Visible = false;
            }
        }
        private void SWButton_Click(object sender, EventArgs e)//导航栏---事务
        {
            SWPanel.Visible = !SWPanel.Visible;
            for (int i = 0; i < 3; i++)
            {
                if (FLP[i].Name != "SWPanel")
                    FLP[i].Visible = false;
            }
            for (int i = 0; i < 4; i++)
            {
                if (PA[i].Name != "panel4")
                    PA[i].Visible = false;
            }
        }
        private void PersonButton_Click(object sender, EventArgs e)//导航栏---个人信息
        {
            if(dataGridView1.Visible == false )
            {
                DataClasses1DataContext DB = new DataClasses1DataContext();
                var result = from L in DB.License
                             where L.LICENSENUM == licensenum
                             select new
                             {
                                 姓名 = L.NAME,
                                 驾照编号 = L.LICENSENUM,
                                 驾照类型 = L.TYPE,
                                 初次领证日期 = L.FIRSTDATE,
                                 有效期限 = L.EXDATE,
                                 记分情况 = L.MARKS.ToString() + "/12"
                             };
                dataGridView1.DataSource = result;
            }
            dataGridView1.Visible = true;
            label3.Visible = true;
        }
        private void KFButton_Click(object sender, EventArgs e)//导航栏---扣分
        {
            if(dataGridView2.Visible == false )
            {
                DataClasses1DataContext DB = new DataClasses1DataContext();
                var result = from A in DB.License
                             join B in DB.Ticket on A.LICENSENUM equals B.LICENSENUM
                             join C in DB.Illegal on B.CODE equals C.CODE
                             where C.DECMARKS != 0
                             select new
                             {
                                 罚单编号 = B.ID,
                                 时间 = B.TIME,
                                 地点 = B.PLACE,
                                 违章内容 = C.CONTENTS,
                                 扣分 = C.DECMARKS
                             };
                dataGridView2.DataSource = result;
            }
            dataGridView2.Visible = true;
            label4.Visible = true;
        }
        private void FDCXButton_Click(object sender, EventArgs e)//导航栏---转向罚单查询界面
        {
            if (dataGridView3.Visible == false) 
            {
                DataClasses1DataContext DB = new DataClasses1DataContext();
                var result = from A in DB.License
                             join B in DB.Ticket on A.LICENSENUM equals B.LICENSENUM
                             join C in DB.Illegal on B.CODE equals C.CODE
                             select new
                             {
                                 罚单编号 = B.ID,
                                 时间 = B.TIME,
                                 地点 = B.PLACE,
                                 违章内容 = C.CONTENTS,
                                 扣分 = C.DECMARKS,
                                 罚款金额 = C.PAY,
                                 缴费期限 = B.EXDATE,
                                 是否缴费 = B.STATE == 1 ? "已缴费" : "未缴费"
                             };
                dataGridView3.DataSource = result;
            }
            panel2.Visible = !panel2.Visible;
            dataGridView3.Visible = true;            
        }
        private void CXbutton1_Click(object sender, EventArgs e)//罚单查询---按时间查询
        {
            DataClasses1DataContext DB = new DataClasses1DataContext();
            if (comboBox1.SelectedItem != null)
            {
                var result = from A in DB.License
                             join B in DB.Ticket on A.LICENSENUM equals B.LICENSENUM
                             join C in DB.Illegal on B.CODE equals C.CODE
                             where B.TIME.ToString().Substring(0, 4).Equals(comboBox1.SelectedItem.ToString())
                             select new
                             {
                                 罚单编号 = B.ID,
                                 时间 = B.TIME,
                                 地点 = B.PLACE,
                                 违章内容 = C.CONTENTS,
                                 扣分 = C.DECMARKS,
                                 罚款金额 = C.PAY,
                                 缴费期限 = B.EXDATE,
                                 是否缴费 = B.STATE == 1 ? "已缴费" : "未缴费"
                             };
                dataGridView3.DataSource = result;
            }
            else
            {
                var result = from A in DB.License
                             join B in DB.Ticket on A.LICENSENUM equals B.LICENSENUM
                             join C in DB.Illegal on B.CODE equals C.CODE
                             select new
                             {
                                 罚单编号 = B.ID,
                                 时间 = B.TIME,
                                 地点 = B.PLACE,
                                 违章内容 = C.CONTENTS,
                                 扣分 = C.DECMARKS,
                                 罚款金额 = C.PAY,
                                 缴费期限 = B.EXDATE,
                                 是否缴费 = B.STATE == 1 ? "已缴费" : "未缴费"
                             };
                dataGridView3.DataSource = result;
            }
        }
        private void CXbutton2_Click(object sender, EventArgs e)//罚单查询---按编号查询
        {
            DataClasses1DataContext DB = new DataClasses1DataContext();
            try { int.Parse(textBox1.Text); }
            catch { MessageBox.Show("请输入数字"); }
            if (textBox1.Text != null)
            {
                var result = from A in DB.License
                             join B in DB.Ticket on A.LICENSENUM equals B.LICENSENUM
                             join C in DB.Illegal on B.CODE equals C.CODE
                             where B.ID.ToString().Equals(textBox1.Text)
                             select new
                             {
                                 罚单编号 = B.ID,
                                 时间 = B.TIME,
                                 地点 = B.PLACE,
                                 违章内容 = C.CONTENTS,
                                 扣分 = C.DECMARKS,
                                 罚款金额 = C.PAY,
                                 缴费期限 = B.EXDATE,
                                 是否缴费 = B.STATE == 1 ? "已缴费" : "未缴费"
                             };
                dataGridView3.DataSource = result;
            }
            else
            {
                var result = from A in DB.License
                             join B in DB.Ticket on A.LICENSENUM equals B.LICENSENUM
                             join C in DB.Illegal on B.CODE equals C.CODE
                             select new
                             {
                                 罚单编号 = B.ID,
                                 时间 = B.TIME,
                                 地点 = B.PLACE,
                                 违章内容 = C.CONTENTS,
                                 扣分 = C.DECMARKS,
                                 罚款金额 = C.PAY,
                                 缴费期限 = B.EXDATE,
                                 是否缴费 = B.STATE == 1 ? "已缴费" : "未缴费"
                             };
                dataGridView3.DataSource = result;
            }
        }
        private void textBox1_TextChanged(object sender, EventArgs e)//罚单查询---编号查询输入框
        {          
                if (textBox1.Text.Length == 8)
                {
                    CXbutton2.Enabled = true;
                }
                else 
                {
                    CXbutton2.Enabled = false;
                }
        }
        private void CZbutton_Click(object sender, EventArgs e)//罚单查询---重置查询条件
        {
            comboBox1.SelectedItem  = null;
            textBox1.Text = null;
            radioButton3.Checked = true;
            DataClasses1DataContext DB = new DataClasses1DataContext();
            var result = from A in DB.License
                         join B in DB.Ticket on A.LICENSENUM equals B.LICENSENUM
                         join C in DB.Illegal on B.CODE equals C.CODE
                         select new
                         {
                             罚单编号 = B.ID,
                             时间 = B.TIME,
                             地点 = B.PLACE,
                             违章内容 = C.CONTENTS,
                             扣分 = C.DECMARKS,
                             罚款金额 = C.PAY,
                             缴费期限 = B.EXDATE,
                             是否缴费 = B.STATE == 1 ? "已缴费":"未缴费"
                         };
            dataGridView3.DataSource = result;
        }
        private void FDJFButton_Click(object sender, EventArgs e)//罚单缴费---转向罚单缴费界面
        {
            var PayId = dataGridView3.SelectedRows[0].Cells[0].Value.ToString();
            var PayState = dataGridView3.SelectedRows[0].Cells[7].Value.ToString();
            if (PayState.Equals("已缴费")) { MessageBox.Show("该罚单已缴费。"); }
            else
            {
                DataClasses1DataContext DB = new DataClasses1DataContext();
                var result = from A in DB.License
                             join B in DB.Ticket on A.LICENSENUM equals B.LICENSENUM
                             join C in DB.Illegal on B.CODE equals C.CODE
                             where B.ID.ToString().Equals(PayId)
                             select new
                             {
                                 罚单编号 = B.ID,
                                 时间 = B.TIME,
                                 地点 = B.PLACE,
                                 违章内容 = C.CONTENTS,
                                 扣分 = C.DECMARKS,
                                 罚款金额 = C.PAY,
                                 缴费期限 = B.EXDATE,
                                 是否缴费 = B.STATE == 1 ? "已缴费" : "未缴费"
                             };
                dataGridView4.DataSource = result;
                for (int i = 0; i < 4; i++)
                {
                    if (PA[i].Name != "panel3")
                        PA[i].Visible = false;
                }
                panel3.Visible = true;
                PaytextBox.Text = dataGridView4.SelectedRows[0].Cells[5].Value.ToString();
            }
        }
        private void dataGridView3_CurrentCellChanged(object sender, EventArgs e)//罚单缴费---禁止未查询先缴费
        {
            if (dataGridView3.CurrentCell != null) 
            {
                FDJFButton.Enabled = true;
            }
        }
        private void dataGridView3_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)//罚单缴费---单元格双击快捷键转向缴费
        {
            FDJFButton_Click(sender,e);
        }
        private void button4_Click(object sender, EventArgs e)//罚单缴费---缴费
        {
            if (dataGridView4.SelectedRows[0].Cells[7].Value.ToString().Equals("已缴费")) { MessageBox.Show("该罚单已缴费，请勿重复付款"); }
            else
            {
                DialogResult dr = MessageBox.Show("确定要付款？", "付款", MessageBoxButtons.YesNo);
                if (dr == DialogResult.Yes)
                {
                    DataClasses1DataContext DB = new DataClasses1DataContext();
                    var editDB = DB.Ticket.Single(p => p.ID == int.Parse(dataGridView4.SelectedRows[0].Cells[0].Value.ToString()));
                    editDB.STATE = 1;
                    DB.SubmitChanges();
                    MessageBox.Show("缴费完成");
                    FDJFButton_Click(sender, e);
                }
            }
        }
        private void radioChange(object sender, EventArgs e)//罚单查询---按是否缴费分类查询的单选框
        {            
            DataClasses1DataContext DB = new DataClasses1DataContext();
            IQueryable result;
            if (radioButton1.Checked == true)
            {                
                result = from A in DB.License
                         join B in DB.Ticket on A.LICENSENUM equals B.LICENSENUM
                         join C in DB.Illegal on B.CODE equals C.CODE
                         where B.STATE == 1
                         select new
                         {
                            罚单编号 = B.ID,
                            时间 = B.TIME,
                            地点 = B.PLACE,
                            违章内容 = C.CONTENTS,
                            扣分 = C.DECMARKS,
                            罚款金额 = C.PAY,
                            缴费期限 = B.EXDATE,
                            是否缴费 = B.STATE == 1 ? "已缴费" : "未缴费"
                         };
            }
            else if (radioButton2.Checked==true)
            {
                result = from A in DB.License
                         join B in DB.Ticket on A.LICENSENUM equals B.LICENSENUM
                         join C in DB.Illegal on B.CODE equals C.CODE
                         where B.STATE == 0
                         select new
                         {
                             罚单编号 = B.ID,
                             时间 = B.TIME,
                             地点 = B.PLACE,
                             违章内容 = C.CONTENTS,
                             扣分 = C.DECMARKS,
                             罚款金额 = C.PAY,
                             缴费期限 = B.EXDATE,
                             是否缴费 = B.STATE == 1 ? "已缴费" : "未缴费"
                         };
            }
            else
            {
                result = from A in DB.License
                         join B in DB.Ticket on A.LICENSENUM equals B.LICENSENUM
                         join C in DB.Illegal on B.CODE equals C.CODE
                         select new
                         {
                             罚单编号 = B.ID,
                             时间 = B.TIME,
                             地点 = B.PLACE,
                             违章内容 = C.CONTENTS,
                             扣分 = C.DECMARKS,
                             罚款金额 = C.PAY,
                             缴费期限 = B.EXDATE,
                             是否缴费 = B.STATE == 1 ? "已缴费" : "未缴费"
                         };
             }
            dataGridView3.DataSource = result;
        }
        private void radioButton1_CheckedChanged(object sender, EventArgs e)//罚单查询---分类查询的单选框发生改变后的转向
        {
            radioChange(sender, e);
        }
        private void radioButton2_CheckedChanged(object sender, EventArgs e)//罚单查询---分类查询的单选框发生改变后的转向
        {
            radioChange(sender, e);
        }
        private void radioButton3_CheckedChanged(object sender, EventArgs e)//罚单查询---分类查询的单选框发生改变后的转向
        {
            radioChange(sender, e);
        }
        private void GSButton_Click(object sender, EventArgs e)//事务办理---转向挂失界面
        {
            panel4.Visible = true;
            DataClasses1DataContext DB = new DataClasses1DataContext();
            var result = from L in DB.License
                            where L.LICENSENUM == licensenum
                            select new
                            {
                                姓名 = L.NAME,
                                驾照编号 = L.LICENSENUM,
                                驾照类型 = L.TYPE,
                                初次领证日期 = L.FIRSTDATE,
                                有效期限 = L.EXDATE,
                                激活状态 = L.STATE == 1 ? "正常" : "挂失"
                            };
            dataGridView5.DataSource = result;
            GSbutton1.Visible = true;
            BBbutton1.Visible = false;
        }
        private void GSbutton1_Click(object sender, EventArgs e)//事务办理---挂失按钮事件触发
        {
            DataClasses1DataContext DB = new DataClasses1DataContext();
            var result = DB.License.Single(p => p.LICENSENUM == licensenum);
            if (result.STATE == 1)
            {
                DialogResult dr = MessageBox.Show("确定要挂失？", "确认框", MessageBoxButtons.YesNo);
                if (dr == DialogResult.Yes)
                {
                    result.STATE = 0;
                    DB.SubmitChanges();
                    MessageBox.Show("挂失完成");
                    GSButton_Click(sender, e);
                }
            }
            else
            { MessageBox.Show("该驾照已经挂失"); }
        }
        private void BBButton_Click(object sender, EventArgs e)//事务办理---补办申请
        {
            DataClasses1DataContext DB = new DataClasses1DataContext();
            var query = from m in DB.Applications 
                        where m.LICENSENUM == licensenum
                        select m;
            var result = from L in DB.License
                         where L.LICENSENUM == licensenum
                         select new
                         {
                             姓名 = L.NAME,
                             驾照编号 = L.LICENSENUM,
                             驾照类型 = L.TYPE,
                             初次领证日期 = L.FIRSTDATE,
                             有效期限 = L.EXDATE,
                             激活状态 = L.STATE == 1 ? "正常" : "挂失",
                             补办申请 = query.Count() > 0 ? "已经申请补办" : "还未申请补办"
                         };
            dataGridView5.DataSource = result;
            GSbutton1.Visible = false;
            BBbutton1.Visible = true;
        }
        private void BBbutton1_Click(object sender, EventArgs e)//事务办理---补办申请--补办操作
        {
            DataClasses1DataContext DB = new DataClasses1DataContext();
            var result = DB.License.Single(p => p.LICENSENUM == licensenum);
            if (result.STATE == 0)
            {
                DialogResult dr = MessageBox.Show("确定要补办？", "确认框", MessageBoxButtons.YesNo);
                if (dr == DialogResult.Yes)
                {
                    Applications app = new Applications()
                    {
                        NAME = result.NAME,
                        LICENSENUM = this.licensenum,
                        APPDATE = System.DateTime.Now
                    };
                    DB.Applications.InsertOnSubmit(app);
                    DB.SubmitChanges();                    
                    MessageBox.Show("补办申请已提交，请等待审核");
                    BBButton_Click(sender, e);
                }
            }
            else
            { MessageBox.Show("该驾照没有挂失，请先挂失后再申请补办"); }
        }
    }
}
